<template>
	<view class="contianer" :class="'wp-' + platform">
		<cu-custom :bgColor="theme.bgnav" :isBack="true" Barborder="solid-bottom">
			<block slot="content">创建预约会议</block>
		</cu-custom>
		<view class="mainpageitem">
			<view class="metting-box">
				<u--form :model="meetForm" ref="uForm" labelPosition="top" labelWidth="auto">
					<u-form-item label="会议主题" prop="name">
						<u-input placeholder="请输入会议主题" v-model="meetForm.title" />
					</u-form-item>
					<u-form-item label="会议内容" prop="name">
						<u-textarea placeholder="请输入会议描述" v-model="meetForm.description" />
					</u-form-item>
					<u-form-item label="预约时间" prop="name" >
						<view style="width:100%;" @click="showTime=true">
							<u--input style="pointer-events:none"
								disabled v-model="meetForm.dueTime"  >
							</u--input>
						</view>
					</u-form-item>
				</u--form>
			</view>
			<view class="flex flex-direction margin-top padding">
				<button class="cu-btn lg bg-limegreen" @tap="handleAddMetting">创建会议</button>
			</view>
		</view>
		<u-datetime-picker
				closeOnClickOverlay
				:show="showTime"
				:minDate="Number(new Date())"
				:formatter="formatter"
				v-model="meetTime"
				@confirm="sureTime"
				@close="showTime=false"
				@cancel="showTime=false"
				mode="datetime"
		></u-datetime-picker>
	</view>
	
</template>

<script>
	import core from '@/core';
	import store from '@/store';
	import * as API_liveKit from '@/api/livekit'
	import {
		
	} from '@/data/common'
	import { createMessage } from './js/common'
	export default {
		data() {
			return {
				theme: core.style,
				text: core.langs,
				platform: core.platform,
				meetForm:{
					
				},
				meetTime: Number(new Date()),
				showTime:false,
			};
		},
		async onLoad() {
			this.$set(this.meetForm,'dueTime',this.$u.date(Number(new Date()), 'yyyy年mm月dd日 hh:MM'))
		},
		computed: {
			
		},
		methods: {
			handleAddMetting(){
				if(this.meetForm.title){
					const params = {
						title: this.meetForm.title,
						description: this.meetForm.description || this.meetForm.title,
						dueTime: this.meetForm.dueTime || this.$u.date(Number(new Date()), 'yyyy年mm月dd日 hh:MM'),
					}
					this.handleOrderMeet(params);
					// let data = JSON.stringify(params);
					// this.naviTo('livekit/metting?type=booking&params=' + encodeURIComponent(data));
				}else{
					return core.toast('请输入会议主题');
				}
			},
			async handleOrderMeet(params){
				let resp = await this.createMeet(params)
				if (!resp.data) return
				const roomResp = await this.handleGetRoomInfo(resp.data)
				if (roomResp && roomResp.code == 200) {
					const messageData = {
						meetType: 'order',
						roomList: [{ id: store.getters.user.id, name: store.getters.user.name }],
						time: '00:00:00',
						roomTag: roomResp.data.tag,
						roomInfo: roomResp.data
					}
					const rCall = (await createMessage(messageData))[0];
					// if (this.$nativeRtc.isUIKitEnable()) {
					// 	// 调用原生插件
					// 	this.$nativeRtc.onJoinMeeting({
					// 		"uid":store.getters.user.id,
					// 		"name":store.getters.user.name,
					// 		"tag":roomResp.data.tag,
					// 		"title":params.title,
					// 		"description":params.description,
					// 		"muted":false,
					// 		"microphoneOn":false,
					// 		"cameraOn":false,
					// 		"fromGroupMode":false,
					// 	})
					// 	return;
					// }
					if(rCall.id) this.naviTo('livekit/livekitInfo?id='+rCall.id);
					// if(rCall.id) this.naviTo('livekit/livekitInfo?id='+rCall.id);
				}
			},
			// 创建一个异步函数createMeet，接收一个参数params
			async createMeet(params){
				uni.showLoading({
					title: '正在创建会议...',
					mask: true
				});
				const resp = await API_liveKit.createMeet(params) //  创建房间
				uni.hideLoading();
				uni.closel
				if (resp.code == 200 && resp.data) {
					return resp
				} else {
					return core.toast('会议创建失败，请联系管理员')
				}
			},
			// 获取房间详情
			async handleGetRoomInfo(roomTag){
				let params = {
					roomTag: roomTag
				}
				const resp = await API_liveKit.getRoomInfo(params)
				if (resp.code == 200) {
					return resp
				} else {
					this.toast('房间不存在')
					return false
				}
			
			},
			sureTime({value}){
				this.$set(this.meetForm,'dueTime',this.$u.date(value, 'yyyy年mm月dd日 hh:MM'))
				this.showTime = false;
			},
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				return value
			},
		}
	};
</script>

<style lang="scss">
	.metting-box{
		width: 90%;
		margin: 40rpx auto;
		border-radius: 20rpx;
		background-color: #fff;
		padding: 20rpx;
	}
	.inputClick{
		pointer-events: none;
	}
</style>